#!/usr/bin/python
#-*- coding: utf-8 -*-

import sys
reload(sys)
import os
import simplejson
import pexpect
from pyipmi import tools as ipmitool

class Ipmi(object):

    def get_cpu1_info(self):
        command = 'ipmitool -I lanplus -H 192.168.0.252 -U USERID -P 123456 sensor get "CPU1 sensor"'
        content, code = pexpect.run(command, withexitstatus=True)
        result = os.popen(command).read()
        str_info = result.split("\n")
        json_dict = simplejson.dumps(str_info)
        dict_info = simplejson.loads(json_dict)
        dict = {}
        for x in dict_info[1:]:
            ret = x.split(':')
            key = ret[0].strip()
            value = ret[0].strip()
            dict[key] = value
            print x

        if code != 0:
            result = {
                'code': code,
                'message': content
            }
        else:
            result = {
                'code': 0,
                'message': 'success',
                'data': {
                    'content': content.strip()
                }
            }
        return result

    def get_cpu2_info(self):
        command = 'ipmitool -I lanplus -H 192.168.0.252 -U USERID -P 123456 sensor get "CPU2 sensor"'
        content, code = pexpect.run(command, withexitstatus=True)
        result = os.popen(command).read()
        str_info = result.split("\n")
        json_dict = simplejson.dumps(str_info)
        dict_info = simplejson.loads(json_dict)
        dict = {}
        for x in dict_info[1:]:
            ret = x.split(':')
            key = ret[0].strip()
            value = ret[0].strip()
            dict[key] = value
            print x

        if code != 0:
            result = {
                'code': code,
                'message': content
            }
        else:
            result = {
                'code': 0,
                'message': 'success',
                'data': {
                    'content': content.strip()
                }
            }
        return result


    def get_cpu_all(self):
        result = {}
        cpu1 = self.get_cpu1_info()
        cpu2 = self.get_cpu2_info()

        result['cpu1'] = cpu1
        result['cpu2'] = cpu2

        return result

if __name__ == '__main__':
    c = Ipmi()
    print c.get_cpu_all()







